package idv.takeshi.software.productline.bookstore.domain.model.book;

import idv.takeshi.software.productline.bookstore.domain.model.valueobject.InternationalText;

import java.math.BigDecimal;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * A model class for representing as a promotion.
 * @author takeshi
 *
 */
@Entity
public abstract class Promotion {
	
	@Id
    @GeneratedValue(strategy = GenerationType.TABLE)
	private Long id;
	
	@Embedded
	private InternationalText name;
	 
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public InternationalText getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(InternationalText name) {
		this.name = name;
	}



	/**
	 * Get the promoted price based on the fixed price of the given book,
	 * and then calculated with this promotion logic.
	 * @param book
	 * @return
	 */
	public abstract BigDecimal getPromotedPrice(Book book);

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (!(obj instanceof Promotion)) {
			     return false;
			   }
		   Promotion rhs = (Promotion) obj;
		   return new EqualsBuilder()
		       .append(this.id, rhs.id)
			   .append(this.name, rhs.name)
		       .isEquals();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(19, 23).
		   append(this.id).
	       append(this.name).
	       toHashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	/**
	 * Get fixedPrice.
	 * also check for book and corresponding fixedPrice shall not be null.
	 * @param book
	 * @return
	 */
	protected static BigDecimal getFixedPrice(Book book) {
		Validate.notNull(book, "book shall always not be null");
		BigDecimal fixedPrice = book.getFixedPrice();
		Validate.notNull(fixedPrice, "fixedPrice shall always not be null");
		return fixedPrice;
	}
	 
}
 
